package simpleftp.ftp;

import com.google.gson.internal.StringMap;

import java.util.ArrayList;
import java.util.List;

/**
 * Class to describe a tree of Files 
 * @param <T>
 */
public class FileTreeNode<T> {
	private T data;
	private List<FileTreeNode<T>> children;

	public FileTreeNode() {
		super();
		children = new ArrayList<FileTreeNode<T>>();
	}

	public FileTreeNode(T data) {
		this();
		setData(data);
	}

	public List<FileTreeNode<T>> getChildren() {
		return this.children;
	}

	public int getNumberOfChildren() {
		return getChildren().size();
	}

	public boolean hasChildren() {
		return (getNumberOfChildren() > 0);
	}

	public void setChildren(List<FileTreeNode<T>> children) {
		this.children = children;
	}

	public void addChild(FileTreeNode<T> child) {
		children.add(child);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public T getData() {
		if (data.getClass() == StringMap.class) {
			StringMap tempData = (StringMap) data;
			FileData fd = new FileData();
			fd.setAbsolutePath(tempData.get("absolutePath").toString());
			fd.setName(tempData.get("name").toString());
			fd.setPath(tempData.get("path").toString());
			data = (T) fd;
		}

		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
}
